package com.jlboot.app.admin.controller.sys;

import com.jlboot.core.base.PageSearch;
import com.jlboot.app.admin.ui.plugins.layui.LayuiData;
import com.jlboot.core.json.JsonResult;
import com.jlboot.core.model.DataMap;
import com.jlboot.core.util.DateUtil;
import com.jlboot.app.admin.base.BaseController;
import com.jlboot.core.form.FormData;
import com.jlboot.core.plugins.UploadUtil;
import com.jlboot.model.po.sys.SysNotice;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * Created by 51594 on 2020/8/31.
 */
public class SysNoticeController extends BaseController {

    
    public void index() {
        renderText("默认");
    }


    /**
     * 列表
     */
    
    public void SysNoticeList() {
        FormData fd = FormData.n();
        setAttr("form", fd);
        render("SysNoticeList.html");
    }

    /**
     * 列表数据提供API
     */
    
    public void SysNoticeListDataApi() {

        PageSearch ps = PageSearch.n()
                .setTable("sys_notice")
                .orderby("sort desc")
                .initSearch(getRequest());
        Page<SysNotice> pages = ps.paginate(SysNotice.dao);

//        String sql = "from sys_notice ";
//        sql = addSearch(sql, "not_id", "=", "string", getPara("s[keyid]"));
//        sql = addOrderBy(sql, "sort desc");
//
//        Page<SysNotice> pages = SysNotice.dao.paginate(getPage(), getLimit(10), "select *", sql);
        List datalist = pages.getList();
        for (int k = 0; k < datalist.size(); k++) {
            SysNotice st = (SysNotice) datalist.get(k);
            st.put("not_times", DateUtil.getDateHF(st.mod.getDate("not_time")));
        }
        renderJson(LayuiData.newData().setData(datalist).setCount(pages.getTotalRow()));
    }

    /**
     * 添加
     */
    
    public void AddSysNotice() {
        FormData fd = FormData.n().setActions("ADD");
        setAttr("notice", new SysNotice());
        setAttr("form", fd);
        render("ModifySysNotice.html");
    }

    /**
     * 修改
     */
    
    public void EditSysNotice() {
        String not_id = getPara("not_id", "");
        SysNotice o = SysNotice.dao.findById(not_id);
        if (o != null) {
            o.put("not_times", DateUtil.getDateHF(o.mod.getDate("not_time")));
        }
        FormData fd = FormData.n().setActions("EDIT");
        setAttr("form", fd);
        setAttr("notice", o);
        render("ModifySysNotice.html");
    }

    /**
     * 修改API
     */
    
    public void ModifySysNoticeApi() {
        //取得数据
        DataMap data = getDatas(getPara("postData"));
        int not_id = data.getInt("not_id");//主键
        String actions = data.getStr("actions");//action
        if ("EDIT".equals(actions)) {//修改
            boolean results = SysNotice.dao.findById(not_id)
                    .set("not_title", data.getStr("not_title"))
                    .set("not_time", data.getDate("not_time"))
                    .set("data_text", data.getStr("data_text"))
                    .set("data_img1", data.getStr("data_img1"))
                    .set("data_img2", data.getStr("data_img2"))
                    .set("data_img3", data.getStr("data_img3"))
                    .set("data_img4", data.getStr("data_img4"))
                    .set("data_img5", data.getStr("data_img5"))
                    .update();
            renderJson(Json(results, "修改"));
        } else {//添加
            SysNotice sn = new SysNotice()
                    .set("not_title", data.getStr("not_title"))
                    .set("not_time", data.getDate("not_time"))
                    .set("data_text", data.getStr("data_text"))
                    .set("data_img1", data.getStr("data_img1"))
                    .set("data_img2", data.getStr("data_img2"))
                    .set("data_img3", data.getStr("data_img3"))
                    .set("data_img4", data.getStr("data_img4"))
                    .set("data_img5", data.getStr("data_img5"));
            boolean results = sn.save();
            sn.set("sort", sn.getInt("not_id")).update();
            renderJson(Json(results, "添加"));
        }
    }

    /**
     * 修改field API
     */
    
    public void ModifySysNoticeFieldApi() {
        int not_id = getParaToInt("not_id", 0);
        String field = getPara("field");
        String value = getPara("values");
        SysNotice.dao.findById(not_id).set(field, value).update();
        renderJson(JsonResult.okWithMsg("修改成功"));
    }

    /**
     * 删除接口API
     */
    
    public void DelSysNoticeApi() {
//        int not_id = getParaToInt("not_id", 0);
//        if (SysNotice.dao.findById(not_id).delete()) {
//            renderJson(JsonResult.okWithMsg("删除成功"));
//        } else {
//            renderJson(JsonResult.fail( "删除失败"));
//        }
        ///////////////////////////////////

        int not_id = getParaToInt("not_id", 0);
        int counts = Db.queryInt("select count(*) from TABLENAME where not_id=?", not_id);
        if (counts == 0) {
            if (SysNotice.dao.findById(not_id).delete()) {
                renderJson(JsonResult.okWithMsg("删除成功"));
            } else {
                renderJson(JsonResult.fail( "删除失败"));
            }
        } else {
            renderJson(JsonResult.fail( "该记录下有" + counts + "子记录,不能删除"));
        }
    }


    /**
     *
     */
    
    public void PubSysNoticeApi() {
        int not_id = getParaInt("not_id", 0);
        renderJson(DoBasePub(SysNotice.dao, not_id, "pub_tag", "发布"));
    }


    /**
     *
     */
    
    public void TestSysNoticeApi() {
        int not_id = getParaInt("not_id", 0);
        renderJson(DoBasePub(SysNotice.dao, not_id, "test_tag", "发布"));
    }


    /**
     * 图片上传接口
     */
    
    public void UploadFile() {
        renderJson(UploadUtil.n().upload(getRequest(), "notice", true, true));
    }


}

